package com.iemes.controller.order;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.HomeWorkFormMap;
import com.iemes.mapper.orderMaterial.HomeWorkMapper;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/homework/")
public class HomeworkController extends BaseController {
    @Inject
        private HomeWorkMapper homeWorkMapper;


    @RequestMapping("openUI")
    public String openUI(Model model){
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH + "/order/HomeworkrManagement";
    }
    //后台获取主要数据
    @ResponseBody
    @RequestMapping("getHomeworkPage")
    public PageInfo getHomeworkPage(){
        //获取数据
        PageHelper.startPage(1, 10);
        List<HomeWorkFormMap> homeWorkData = homeWorkMapper.getHomeWork();
        PageInfo pageInfo = new PageInfo<>(homeWorkData);
        return pageInfo;
    }
    //打开添加页面
    @RequestMapping("openAddUi")
    public String openAddUi(){
        return Common.BACKGROUND_PATH + "/order/addHomework";
    }
    //添加数据
    @ResponseBody
    @RequestMapping("addData")
    public String addData(){
        HomeWorkFormMap homeWorkFormMap = getFormMap(HomeWorkFormMap.class);
        try {
            //根据成品id获取成品编码
            homeWorkMapper.addEntity(homeWorkFormMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
    //打开编辑页面
    @RequestMapping("openEdit")
    public String openEditUi(Model model,HttpServletRequest request){
        //根据id获取相关数据
        String id = request.getParameter("id");
        model.addAttribute("data", homeWorkMapper.getHomeWorkChose(id));
        return Common.BACKGROUND_PATH + "/order/editHomework";
    }
    //保存修改
    @ResponseBody
    @RequestMapping("saveData")
    public String saveData() throws Exception {
        //获取表单信息
        HomeWorkFormMap homeWorkFormMap = getFormMap(HomeWorkFormMap.class);
        //根据id删除数据
        homeWorkMapper.deleteByAttribute("id", homeWorkFormMap.get("id")+"", HomeWorkFormMap.class);
        homeWorkMapper.addEntity(homeWorkFormMap);
        return "success";
    }
    //删除方法
}
